package com.jyx.reflect.instances.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseXml {

	public static List<Bean> parse(String path) throws DocumentException{
		List<Bean> result = new ArrayList<Bean>();
		
		File file = new File(path);
		
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(file);
		
		// 获取根节点
		Element root = doc.getRootElement();
		List<Element> beanElementList = root.selectNodes("bean");
		for (Element element : beanElementList) {
			// Bean
			Bean bean = new Bean();
			bean.setId(element.attributeValue("id"));
			bean.setClassName(element.attributeValue("class"));
			
			// property
			List<Property> propertieList = new ArrayList<Property>();
			List<Element> propertyElementList = element.selectNodes("property");
			for (Element propertyElement : propertyElementList) {
				Property property = new Property();
				property.setName(propertyElement.attributeValue("name"));
				property.setValue(propertyElement.attributeValue("value"));
				
				propertieList.add(property);
			}
			
			bean.setPropertyList(propertieList);
			
			result.add(bean);
		}
		
		return result;
	}
}
